//
//  WCNMyMessageVC.m
//  WCN
//
//  Created by wsj2015 on 15/9/24.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNMyMessageVC.h"
#import "Macros.h"
#import "MJRefresh.h"
#import "WCNetworkAccessManager.h"
#import "WCPopupLayerManager.h"
#import "WCNMessageContainer.h"
#import "WCNMessageCel.h"
#import "WCInputMessageVC.h"
#import "UITableView+(removeMjrefreshFooter).h"
#import "WCNMessageListCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

#define rows 8
@interface WCNMyMessageVC ()<UITableViewDelegate,UITableViewDataSource>

{
    NSInteger _page;
    NSInteger _totalPage;
    
    NSMutableArray *_dataArray;
    WCNMessageCel *_cell;

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation WCNMyMessageVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _dataArray =[NSMutableArray array];
        self.title =@"留言咨询";
       
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =kAppBg;
    
    
    [self configUI];
    
    
}

- (void)configUI
{
     weakSelf_define;
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNMessageListCell" bundle:nil] forCellReuseIdentifier:@"WCNMessageListCellId"];
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        _page =1;
        [weakSelf getMessageListIsRecover:YES];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        _page ++;
        [weakSelf getMessageListIsRecover:NO];
    }];
    
    

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView.header beginRefreshing];

}

- (void)getMessageListIsRecover:(BOOL)isCover
{
//    "page": 1,
//    "rows": 2,
//    "nurseId": 1
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] =@(_page);
    params[@"rows"]= @(rows);
    params[@"nurseId"] =@(self.nurseId);
    
    MBProgressHUD *hud =showHud;
    weakSelf_define;
    [[WCNetworkAccessManager sharedMg]POST:kMyMessageUrl_part parameters:params success:^(NSDictionary *responseObject) {
        
        if (isCover) {
            [_dataArray removeAllObjects];
        }
       
       
        NSDictionary *messDict =responseObject[@"data"];
      [UITableView remvoMJRefreshFooter:_page totalPage:[messDict[@"totalPage"] integerValue] forTableView:weakSelf.tableView withFootFreshBlock:^{
          _page ++;
          [weakSelf getMessageListIsRecover:NO];
      }];
        
        for (NSDictionary *dict in messDict[@"result"]) {
            WCNMessageContainer *messContainer =[[WCNMessageContainer alloc]init];
            [messContainer setValuesForKeysWithDictionary:dict];
            [_dataArray addObject:messContainer];
        
        }
        //自动判断 无数据 则显示无记录视图
        [weakSelf loadNOrecodView:weakSelf.tableView dataArray:_dataArray];
        [weakSelf.tableView reloadData];
        
     } failure:nil netError:nil always:^{
         
         [weakSelf.tableView.header endRefreshing];
         [weakSelf.tableView.footer endRefreshing];
         
         [hud hide:YES];
     }];



}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNMessageContainer *message =_dataArray[indexPath.row];
    return  [tableView fd_heightForCellWithIdentifier:@"WCNMessageListCellId" configuration:^(WCNMessageListCell * cell) {
        cell.model = message;
        
    }];
    
 
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WCNMessageListCell *cell =[WCNMessageListCell CellWithTableView:tableView indexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    WCNMessageContainer *message =_dataArray[indexPath.row];
    [cell setModel:message];
  
   
    return cell;
   
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNMessageContainer *message =_dataArray[indexPath.row];
//    if (message.replyContent) {
//        [[WCPopupLayerManager shareMg]showTextOnly:@"您已经回复过了~" withView:self.view];
//        return;
//    } 
    WCInputMessageVC *vc =[[WCInputMessageVC alloc]init];
    vc.messageId =[message.wcId integerValue];
    [self.navigationController pushViewController:vc animated:YES];
   

}

@end
